package com.cse.core;

import java.io.*;
import java.text.Collator;
import java.util.*;

public class BuildWordVacabulary {

	private String wordvacabularypath = null;

	private ArrayList wordlist = new ArrayList();

	public void loadProductFromDirectory(String[] dirs) throws IOException {
		String fullname = null;
		try {
			if (wordvacabularypath == null) {
				throw new IOException("�ʿ��ŵ��ļ�û��ָ�����޷���ɣ�");
			}

			for (int i = 0; i < dirs.length; i++) {

				String folder = dirs[i];
				File foo = new File(folder);
				String[] files = foo.list();

				for (int j = 0; j < files.length; j++) {
					File f = new File(foo, files[j]);

					 fullname = f.getName();
					// ��һ��ȡ�ò�Ʒ�����
					// �ڶ���ȡ�ò�Ʒ���ͺ�
					System.out.println(fullname);
					
					if (fullname.indexOf("-") == fullname.lastIndexOf("-")) {
						continue;
					}

					String type = fullname.substring(fullname.indexOf("-") + 1,
							fullname.lastIndexOf("-"));
					String name = fullname.substring(0, fullname.indexOf("-"));

					if (!wordlist.contains(type)) {
						wordlist.add(type);
					}

					if (!wordlist.contains(name)) {
						wordlist.add(name);
					}
					if (!wordlist.contains(name+type)) {
						wordlist.add(name+type);
					}
				}

			}

			Collections.sort(wordlist);
			writeToFile();
		} catch (Exception e) {
			System.out.println(fullname);
			e.printStackTrace();
		}
	}

	private void writeToFile() throws IOException {

		BufferedWriter writer = new BufferedWriter(new FileWriter(
				wordvacabularypath));

		for (int i = 0; i < wordlist.size(); i++) {
			String type = (String)wordlist.get(i);
			writer.write((String) wordlist.get(i));
			writer.newLine();
		}

		writer.close();

	}

	public void setWordvacabularyPath(String wordvacabularypath) {
		this.wordvacabularypath = wordvacabularypath;
	}

	public static void main(String[] args) throws IOException {
		BuildWordVacabulary builder = new BuildWordVacabulary();
		builder.setWordvacabularyPath("d:\\data\\word.txt");
		builder.loadProductFromDirectory(new String[] { "d:\\product\\tom\\mobile\\" });
	}

}
